{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Known error: This notebook requires [yt](https://yt-project.org/) to visualize the results. Yt needs to be updated to work properly first. Updates are currently being made to yt's frontends to make this PyNE integration work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from yt.mods import *\n",
    "import h5py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If necessary files aren't in the current dir, download them\n",
    "import os\n",
    "import requests\n",
    "\n",
    "if not os.path.isfile(\"mcnp_n_impr_fluka.h5m\"):\n",
    "    r = requests.get(\"http://data.pyne.io/mcnp_n_impr_fluka.h5m\")\n",
    "    with open(\"mcnp_n_impr_fluka.h5m\", \"wb\") as outfile:\n",
    "        outfile.write(r.content)\n",
    "    \n",
    "if not os.path.isfile(\"fng_usrbin22.h5m\"):\n",
    "    # fng_usrbin22.h5m is a large file\n",
    "    # needs to be downloaded in chunks\n",
    "    r = requests.get(\"http://data.pyne.io/fng_usrbin22.h5m\", stream=True)\n",
    "    with open(\"fng_usrbin22.h5m\" , 'wb') as file:\n",
    "        for chunk in r.iter_content(chunk_size=1024):\n",
    "            if chunk:\n",
    "                file.write(chunk)\n",
    "                file.flush()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "yt : [INFO     ] 2015-02-19 07:12:03,396 Parameters: current_time              = 0.0\n",
      "yt : [INFO     ] 2015-02-19 07:12:03,396 Parameters: domain_dimensions         = [ 99.    91.53  98.4 ]\n",
      "yt : [INFO     ] 2015-02-19 07:12:03,397 Parameters: domain_left_edge          = [-49.5 -14.1 -49.2]\n",
      "yt : [INFO     ] 2015-02-19 07:12:03,399 Parameters: domain_right_edge         = [ 49.5   77.43  49.2 ]\n",
      "yt : [INFO     ] 2015-02-19 07:12:03,400 Parameters: cosmological_simulation   = 0.0\n"
     ]
    }
   ],
   "source": [
    "# Load data file\n",
    "pf = load(\"fng_usrbin22.h5m\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "yt : [INFO     ] 2015-02-19 07:12:08,303 Loading field plugins.\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,304 Loaded angular_momentum (8 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,305 Loaded astro (15 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,306 Loaded cosmology (22 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,308 Loaded fluid (64 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,310 Loaded fluid_vector (96 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,311 Loaded geometric (112 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,312 Loaded local (112 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,313 Loaded magnetic_field (120 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,314 Loaded my_plugins (120 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:08,315 Loaded species (122 new fields)\n",
      "yt : [INFO     ] 2015-02-19 07:12:10,477 xlim = -49.500000 49.500000\n",
      "yt : [INFO     ] 2015-02-19 07:12:10,477 ylim = -14.100000 77.430000\n",
      "yt : [INFO     ] 2015-02-19 07:12:10,479 Making a fixed resolution buffer of (('moab', 'TALLY_TAG')) 800 by 800\n",
      "yt : [INFO     ] 2015-02-19 07:12:15,857 xlim = -49.500000 49.500000\n",
      "yt : [INFO     ] 2015-02-19 07:12:15,858 ylim = -14.100000 77.430000\n",
      "yt : [INFO     ] 2015-02-19 07:12:15,860 Making a fixed resolution buffer of (('moab', 'TALLY_TAG')) 800 by 800\n",
      "yt : [INFO     ] 2015-02-19 07:12:15,886 Making a fixed resolution buffer of (('moab', 'TALLY_TAG')) 800 by 800\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\"><br>"
      ],
      "text/plain": [
       "<yt.visualization.plot_window.AxisAlignedSlicePlot at 0x109503cd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Create the desired slice plot\n",
    "s = SlicePlot(pf, 'z', ('moab','TALLY_TAG'), origin='lower-native')\n",
    "# Load the facet file\n",
    "f = h5py.File(\"mcnp_n_impr_fluka.h5m\", \"r\")\n",
    "# Get the triangle vertices\n",
    "coords = f[\"/tstt/nodes/coordinates\"][:]\n",
    "conn = f[\"/tstt/elements/Tri3/connectivity\"][:]\n",
    "points = coords[conn-1]\n",
    "# Annotate slice-triangle intersection contours to the plot\n",
    "s.annotate_triangle_facets(points, plot_args={\"colors\": 'black'})\n",
    "s.display()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
